$(function() {
    layui.form.verify({
        // a.密码校验规则和错误消息
        pwd: [/^\S{6,12}$/, '密码长度为：6-12'],
        diffPwd: function() {
            const oldPwdStr = $('.layui-form [name=oldPwd]').val()
            if (oldPwdStr === newPwdStr) {
                return '新旧密码不能一致'
            }
        },
        // b.注册表单验证
        repwd(repwdValue) {
            const pwdStr = $('.layui-form [name=newPwd]')
            if (pwdStr !== repwdValue) return '两次密码输入不一致'
        }
    })
    $('.layui-form').on('submit', dosubmit)
})

function dosubmit(e) {
    e.preventDefault()
    let data = layui.form.val('userForm')
    data.reNewPwd = null
    $.ajax({
        method: 'post',
        url: '/my/updatepwd',
        data,
        success(res) {
            if (res.status === 1) return layui.layer.alert(res.message)
            layui.layer.alert('重置密码成功', function() {
                localStorage.removeItem('token')
                window.parent.location.href = '/login.html'
            })
        }
    })
}